layui.use(['form', 'layer', 'table'], function () {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		table = layui.table,
		company_id = sessionStorage["company_id"],
		user_id = sessionStorage["userId"];
	if (user_id == 3) {
		loadCompanyList(form, $);
		$('.company').removeClass("layui-hide");
	}
	loadSelect(form, $, "educationType/getEducationType.php", ".eductionType")
	loadSelect(form, $, "cengci/getCengci.php", ".gradation")
	form.render('select')
	//学历信息列表
	var tableIns = table.render({
		elem: '#secondDegreeList',
		url: baseUrl + 'secondDegree/getSecondDegrees.php',
		where: {
			company_id
		},
		skin: 'row',
		even: true, //开启隔行背景
		toolbar: true,
		defaultToolbar: ['filter'],
		cellMinWidth: 95,
		page: true,
		limits: [10, 15, 20, 25],
		limit: 10,
		id: "secondDegreeListTable",
		cols: [
			[
				{
					title: '序号',
					type: "numbers"
				},
				{
					field: 'id',
					title: 'ID',
					hide: true
				},
				{
					field: 'produ',
					title: '学校',
				},
				{
					field: 'ject',
					title: '专业',
				},
				{
					field: 'level',
					title: '层次ID',
					hide: true
				},
				{
					field: 'cengci_name',
					title: '层次',
					templet: function (d) {
						return `<span style="color: #e4393c;">${d.cengci_name}</span>`;
					}
				},
				{
					field: 'xuezhi',
					title: '学制',
				},
				{
					field: 'price',
					title: '合作价格',
					hide: true,
					templet: function (d) {
						if (sessionStorage["scale"] == "4") {
							return '<span style="color: #e4393c;">' + "0" + '</span>';
						} else if (sessionStorage["scale"] == "1" || sessionStorage["scale"] == "2" || sessionStorage["scale"] == "3") {
							return '<span style="color: #e4393c;">' + d.price + '</span>';
						}
					}
				},
				{
					field: 'Agencyprice',
					title: '代理价格',
					hide: true,
					templet: function (d) {
						return '<span style="color: #e4393c;">' + d.Agencyprice + '</span>';
					}
				}, {
					field: 'Externalprice',
					title: '市场价格',
				}, {
					field: 'type',
					title: '学历类型ID',
					hide: true
				}, {
					field: 'educationtype_name',
					title: '学历类型',
					templet: function (d) {
						return `<span style="color: #e4393c;">${d.educationtype_name}</span>`;
					}
				}, {
					field: 'remarks',
					title: '备注',
				}, {
					field: 'validYear',
					title: '适用年份',
				},
				{
					title: '操作',
					width: 150,
					templet: '#secondDegreeListBar',
					fixed: "right",
					align: "center"
				}
			]
		],
		done: function (res, curr, count) {// done 数据表格加载完成
			if (sessionStorage['scale'] == 1 || sessionStorage['scale'] == 2) {
				$('.addNews_btn').css('display', 'inline-block')
			}
		}
	});
	$('.search').on('keyup', function (event) {
		if (event.keyCode != 13) return
		$(".search_btn").click()
	})
	//搜索
	$(".search_btn").on("click", function () {
		table.reload("secondDegreeListTable", {
			page: {
				curr: 1 //重新从第 1 页开始
			},
			where: {
				rponame: $('.searchSchoolName').val(),
				typephj: $('.eductionType').val(),
				major: $('.major').val(),
				gradation: $('.gradation').val(),
				company_id: user_id == 3 ? $("#company").val() : company_id
			}
		})
	});
	//添加用户
	function editSecondDegree(edit) {
		var index = layui.layer.open({
			title: "学历信息",
			type: 2,
			area: ['80%', '80%'],
			fixed: false, //不固定
			maxmin: true,
			shadeClose: true,
			content: "secondDegreeEdit.html",
			success: async function (layero, index) {
				var body = layui.layer.getChildFrame('body', index);
				(await loadSelect(form, $, 'educationType/getEducationType.php', '', 'edit')).forEach(item => {
					if (index != 0) body.find('.eductionType').append(new Option(item.name, item.id))
				});
				(await loadSelect(form, $, 'cengci/getCengci.php', '', 'edit')).forEach(item => {
					if (index != 0) body.find('.gradation').append(new Option(item.name, item.id))
				});
				if (edit) {//修改
					body.find(".addOrUpdate").val(1);
					body.find(".secondDegreeId").val(edit.id);
					body.find('.schoolName').val(edit.produ);
					body.find('.price').val(edit.price);
					body.find('.agencyPrice').val(edit.Agencyprice);
					body.find('.externalPrice').val(edit.Externalprice);
					body.find('.xuezhi').val(edit.xuezhi);
					body.find('.major').val(edit.ject);
					body.find('.remarks').val(edit.remarks);
					body.find('.eductionType').val(edit.type);
					body.find('.gradation').val(edit.level);
					body.find('.validYear').val(edit.validYear);
				} else {//添加
					body.find(".addOrUpdate").val(0);
				}
				setTimeout(function () {
					layui.layer.tips('点击此处返回学历列表', '.layui-layer-setwin .layui-layer-close', {
						tips: 3
					});
				}, 500)
			}
		})
		//layui.layer.full(index);
		sessionStorage.setItem("index", index);
		//改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
	}
	$(".addNews_btn").click(function () {
		editSecondDegree();
	})
	//列表操作
	table.on('tool(secondDegreeList)', function (obj) {
		var layEvent = obj.event,
			data = obj.data;
		if (layEvent === 'edit') { //编辑
			editSecondDegree(data);
		} else if (layEvent === 'del') { //删除
			layer.confirm('确定删除学历信息 ' + data.produ + ' ？', {
				icon: 3,
				title: '删除确认'
			}, function (index) {
				$.ajax({
					url: baseUrl + "secondDegree/deleteSecondDegree.php",
					data: {
						id: data.Id
					},
					type: "POST",
					dataType: "text",
					success: function (data) {
						if (data == "1") {
							tableIns.reload();
							layer.close(index);
							layer.msg('删除学历信息成功！')
						} else {
							layer.msg('删除学历信息失败！')
						}
					}
				})
			});
		}
	});

	table.on('rowDouble(secondDegreeList)', function (obj) {
		editSecondDegree(obj.data);
	});

	function getEductionType() {
		let str = `<option value="">学历类型</option>`
		map6.forEach(function (item, index) {
			if (index != 0) str += `<option value="${index}">${item}</option>`
		})
		return str
	}
	function getGradation() {
		let str = `<option value="">层次</option>`
		map8.forEach(function (item, index) {
			if (index != 0) str += `<option value="${index}">${item}</option>`
		})
		return str
	}
})
